package com.bc.ui;


import java.util.HashMap;

import com.bc.R;
import com.bc.db.dao.WarnDao;
import com.bc.service.SimpleRequestListener;
import com.bc.service.deal.ServiceCategory;
import com.bc.service.deal.ServiceFractory;
import com.bc.utils.CommonUtils;
import com.bc.utils.Constants;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class DetailActivity extends BaseActivity {
	private TextView tvSaleTotal, tvLastSale, tvBuy, tvSell, tvHigh, tvLow;
	private ServiceCategory sc;
	String btcType;
	String btcName;
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		
		setContentView(R.layout.activity_detail);
		
		Intent intent = getIntent();
		ActionBar actionBar = getSupportActionBar();
		actionBar.setDisplayShowHomeEnabled(true);
		actionBar.setDisplayHomeAsUpEnabled(true);
		
		btcName = intent.getStringExtra(Constants.BTC_NAME_COL);
		actionBar.setTitle(btcName);
		
		btcType = intent.getStringExtra(Constants.BTC_TYPE_COL);
		
		tvSaleTotal = (TextView)findViewById(R.id.sale_total);
		tvSaleTotal.setText(intent.getStringExtra(Constants.SALE_VOLUME));
		
		tvLastSale = (TextView)findViewById(R.id.last_sale);
		tvLastSale.setText(CommonUtils.DisplayFormatString(btcType, intent.getStringExtra(Constants.LAST_SALE_PRICE), this));
		
		tvBuy = (TextView)findViewById(R.id.buy);
		tvBuy.setText(CommonUtils.DisplayFormatString(btcType, intent.getStringExtra(Constants.BUY_ONE_PRICE), this));
		
		tvSell = (TextView)findViewById(R.id.sell);
		tvSell.setText(CommonUtils.DisplayFormatString(btcType, intent.getStringExtra(Constants.SELL_ONE_PRICE), this));
		
		tvHigh = (TextView)findViewById(R.id.high);
		tvHigh.setText(CommonUtils.DisplayFormatString(btcType, intent.getStringExtra(Constants.SELL_HIGH_PRICE), this));
		
		tvLow = (TextView)findViewById(R.id.low);
		tvLow.setText(CommonUtils.DisplayFormatString(btcType, intent.getStringExtra(Constants.SELL_LOW_PRICE), this));
		
		int warn_id = getIntent().getIntExtra("warn_position", -1);
		if (warn_id != -1) {
			WarnDao.getInstance(DetailActivity.this).updateWarnEnable(warn_id, 0);
		}
	}
	
	private MenuItem refreshMenuItem;
	private View view;
	boolean firstRefresh = false;
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			overridePendingTransition(R.anim.base_stay_orig, R.anim.push_out_bottom);
			break;
		case R.id.action_refresh:
			MenuItemCompat.setActionView(item, R.layout.action_progressbar);
			refreshData();
			break;
		case R.id.action_warn:
			Intent intent = new Intent(DetailActivity.this, WarnListActivity.class);
			intent.putExtra(Constants.BTC_TYPE_COL, btcType);
			intent.putExtra(Constants.BTC_NAME_COL, btcName);
			startActivity(intent);
			overridePendingTransition(R.anim.push_in_right, R.anim.push_out_left);
		default:
			break;
		}
		return true;
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_detail, menu);
		refreshMenuItem = menu.findItem(R.id.action_refresh);
		view = MenuItemCompat.getActionView(refreshMenuItem);
		return true;
	}
	
	
	public void refreshData() {
		if (sc == null) {
			sc = ServiceFractory.createService(this, getIntent().getStringExtra(Constants.BTC_TYPE_COL));
		}
		sc.loadBtcData(new SimpleRequestListener(this){
			@Override
			public void onComplete(int errorCode) {
				MenuItemCompat.setActionView(refreshMenuItem, view);
				super.onComplete(errorCode);
				if (errorCode == 0) {
					HashMap<String, Object> hashMap = sc.getServieResponse().getBtcNews();
					tvSaleTotal.setText(CommonUtils.ToString(hashMap.get(Constants.SALE_VOLUME)));
					
					Object sellObject = hashMap.get(Constants.SELL_ONE_PRICE);
					String sellPrice = CommonUtils.ToString(sellObject);
					tvSell.setText(CommonUtils.DisplayFormatString(btcType, sellPrice, DetailActivity.this));
					
					tvLastSale.setText(CommonUtils.DisplayFormatString(btcType, 
							CommonUtils.ToString(hashMap.get(Constants.LAST_SALE_PRICE)), DetailActivity.this));
				
					tvBuy.setText(CommonUtils.DisplayFormatString(btcType, 
							CommonUtils.ToString(hashMap.get(Constants.BUY_ONE_PRICE)), DetailActivity.this));
					
					tvHigh.setText(CommonUtils.DisplayFormatString(btcType, 
							CommonUtils.ToString(hashMap.get(Constants.SELL_HIGH_PRICE)), DetailActivity.this));
					
					tvLow.setText(CommonUtils.DisplayFormatString(btcType, 
							CommonUtils.ToString(hashMap.get(Constants.SELL_LOW_PRICE)), DetailActivity.this));
				}
			}
		});
	}
	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
		overridePendingTransition(R.anim.base_stay_orig, R.anim.push_out_bottom);
	}
}
